/*
 * SelectLocationDialog.java
 *
 * Created on August 22, 2007, 12:25 AM
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.admin.gui.dialogs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import org.fpdev.core.basenet.BNode;
import org.fpdev.apps.admin.AdminClient;
import org.fpdev.apps.admin.SpecialNodeClickHandler;

/**
 *
 * @author demory
 */
public class SelectLocationDialog extends JDialog implements ActionListener {

  protected AdminClient av_;
  protected JTextField name_;
  private JButton okBtn_,  importBtn_,  cancelBtn_;

  public SelectLocationDialog(AdminClient cv) {
    super(cv.getGUI(), "Select Location");

    av_ = cv;


    name_ = new JTextField(20);

    okBtn_ = new JButton("OK");
    okBtn_.addActionListener(this);
    importBtn_ = new JButton("From node...");
    importBtn_.addActionListener(this);
    cancelBtn_ = new JButton("Cancel");
    cancelBtn_.addActionListener(this);

    JPanel btnRow = new JPanel();
    btnRow.setLayout(new BoxLayout(btnRow, BoxLayout.X_AXIS));
    btnRow.add(okBtn_);
    btnRow.add(Box.createHorizontalStrut(5));
    btnRow.add(importBtn_);
    btnRow.add(Box.createHorizontalStrut(5));
    btnRow.add(cancelBtn_);

    JPanel topPanel = new JPanel();
    topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));

    topPanel.add(new JLabel("Location:"));
    name_.setAlignmentX(LEFT_ALIGNMENT);
    topPanel.add(name_);

    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

    topPanel.setAlignmentX(CENTER_ALIGNMENT);
    mainPanel.add(topPanel);
    mainPanel.add(Box.createVerticalStrut(6));
    btnRow.setAlignmentX(CENTER_ALIGNMENT);
    mainPanel.add(btnRow);

    mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(mainPanel);

    this.pack();
    this.setLocation((cv.getGUI().getWidth() / 2) - getWidth() / 2 + cv.getGUI().getX(),
            (cv.getGUI().getHeight() / 2) - getHeight() / 2 + cv.getGUI().getY());
    setVisible(true);
  }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == okBtn_) {
      setVisible(false);
      okClicked();
    } else if (e.getSource() == importBtn_) {
      //cv_.getGUI().setSpecialClickAction(AViewGUI.SPECIAL_SELNODE_LM);			
      av_.expectSpecialNodeClick(new LocationNodeHandler());
      av_.getGUI().toFront();
    } else if (e.getSource() == cancelBtn_) {
      setVisible(false);
    }
  }

  protected void okClicked() {
  }

  private void selectedNode(BNode node) {
    name_.setText("%" + node.getID() + "%");
    av_.clearSpecialNodeClickHandler();
  }

  public void setName(String name) {
    name_.setText(name);
  }

  private class LocationNodeHandler implements SpecialNodeClickHandler {

    public void handleNode(BNode node) {
      selectedNode(node);
    }
  }
}
